package com.zzm.validate.validator;

import java.util.Date;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.util.ValidatorUtils;

public class TypeValidator {

	public static Integer validateInt(Object bean, Field field) {   
		String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		Integer intValue = GenericTypeValidator.formatInt(value);
		return intValue;
	}
	
	/**
	 * 使用此验证方法一般要在配置xml中声明
	 * <var>
			<var-name>datePattern</var-name>
			<var-value>yyyy-mm-dd</var-value>
		</var>
		<var>
			<var-name>strict</var-name>
			<var-value>true</var-value>
		</var>
		如果不配置，默认是如上配置
	 */
	public static Date validateDate(Object bean, Field field) {
		String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		String pattern = field.getVarValue("datePattern");
		String strict = field.getVarValue("strict");
		boolean bStrict = true;
		if(null == pattern || pattern.length() < 0) {
			pattern = "yyyy-mm-dd";
		}
		if(null != strict && strict.length() > 0) {
			bStrict = new Boolean(bStrict);
		}
		Date date = GenericTypeValidator.formatDate(value, pattern, bStrict);
		return date;
	}
	
	
	
	
}
